package com.syx.migration.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syx.migration.domain.MgnLinkId;
import com.syx.migration.mapper.MgnLinkIdMapper;
import com.syx.migration.service.MgnLinkIdService;
import com.syx.migration.utils.TimeIdWorker;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @author Administrator
 * @date 2024/6/5 9:03
 * @description: 新老相同id对应
 */
@RequiredArgsConstructor
@Service
public class MgnLinkIdServiceImpl implements MgnLinkIdService {

    private final MgnLinkIdMapper baseMapper;

    @Override
    public Boolean insert(String newId, String oldId, String tbName) {
        return baseMapper.insert(new MgnLinkId(newId, oldId, tbName)) > 0;
    }

    /**
     * 批量存储
     */
    @Override
    public Boolean batchInsert(List<String> newId, List<String> oldId, String tbName) {
        List<MgnLinkId> mgnLinkIds = new ArrayList<>();
        for (int i = 0; i < newId.size(); i++) {
            MgnLinkId mgnLinkId = new MgnLinkId();
            mgnLinkId.setNewId(newId.get(i));
            mgnLinkId.setTableName(tbName);
            mgnLinkId.setOldId(oldId.get(i));
            mgnLinkIds.add(mgnLinkId);
        }
        return this.batchInsert(mgnLinkIds);
    }

    /**
     * 批量存储
     */
    @Override
    public Boolean batchInsert(List<MgnLinkId> mgnLinkIds) {
        return baseMapper.insertBatch(mgnLinkIds);
    }

    /**
     * 获取新系统的id
     */
    @Override
    public String getNewIdByOldId(String tbName, String oldId) {
        MgnLinkId mgnLinkId = baseMapper.selectOne(new LambdaQueryWrapper<MgnLinkId>()
                .eq(MgnLinkId::getTableName, tbName)
                .eq(MgnLinkId::getOldId, oldId)
                .last("limit 1"));
        if (Objects.isNull(mgnLinkId)){
            throw new RuntimeException("未找到新系统的id：" + tbName + "-" + oldId);
        }
        return mgnLinkId.getNewId();
    }

    /**
     * 获取指定表名称所有关联集合
     */
    @Override
    public List<MgnLinkId> getListByTbName(String tbName) {
        return baseMapper.selectList(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
    }

    /**
     * 获取map集合的表名称对应数据 k - oldId v-newId
     */
    @Override
    public Map<String, String> getMapByTbName(String tbName) {
        HashMap<String, String> map = new HashMap<>();
        List<MgnLinkId> list = this.getListByTbName(tbName);
        list.forEach(item ->{
            map.put(item.getOldId(), item.getNewId());
        });
        return map;
    }

    /**
     * 根据老表id集合获取新表id集合
     */
    @Override
    public List<String> getNewIdsByOldIds(List<String> oldIds, String tbName) {
        if (CollUtil.isEmpty(oldIds)){
            return new ArrayList<>();
        }
        List<MgnLinkId> mgnLinkIds = baseMapper.selectList(new LambdaQueryWrapper<MgnLinkId>().in(MgnLinkId::getOldId, oldIds));
        if (CollUtil.isNotEmpty(mgnLinkIds)){
            List<String> newIds = new ArrayList<>();
            mgnLinkIds.forEach(item ->{
                newIds.add(item.getNewId());
            });
            return newIds;
        }
        return new ArrayList<>();
    }

    @Override
    public Boolean tbDelete(String tbName) {
        return baseMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName)) > 0;
    }
}
